/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.service.shop.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.business.dao.shop.CereShopCommentDAO;
import com.shop.cereshop.business.page.shop.PlatformBusiness;
import com.shop.cereshop.business.page.shop.ShopComment;
import com.shop.cereshop.business.param.shop.CommentParam;
import com.shop.cereshop.business.service.shop.CereShopCommentService;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.utils.StringUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereShopCommentServiceImpl implements CereShopCommentService {
    @Autowired
    private CereShopCommentDAO cereShopCommentDAO;
    @Override
    public Page<ShopComment> getAll(CommentParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<ShopComment> list=cereShopCommentDAO.getAll(param);

        PageInfo<ShopComment> pageInfo=new PageInfo<>(list);
        Page page=new Page(pageInfo.getList(),pageInfo.getTotal());
        return page;
    }

    @Override
    public ShopComment getById(Long commentId) {
        return cereShopCommentDAO.selectByPrimaryKey(commentId);
    }

    @Override
    public void reply(ShopComment comment, PlatformBusiness user) {
        //回复之前修改回复的那条评论的状态为一回复
        ShopComment comment1=cereShopCommentDAO.selectByPrimaryKey(comment.getParentId());
        if(comment1 != null){
            comment1.setReplyState(comment.getReplyState());
            cereShopCommentDAO.updateByPrimaryKeySelective(comment1);
        }
        comment.setBuyerUserId(user.getBusinessUserId());
        comment.setReplyState(0);
        comment.setCreateTime(TimeUtils.yyMMddHHmmss());
        cereShopCommentDAO.insertSelective( comment);
    }

    @Override
    public void update(ShopComment param) {
        cereShopCommentDAO.updateByPrimaryKeySelective(param);
    }
}
